﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using TeachMe.Application.Data;

namespace TeachMe.Application
{
    [Flags]
    public enum EditedDescriptorValueType
    {
        From = 1,
        To = 2,
        Example = 4,
        Priority = 8
    }

    public class EditedEntryDescriptor
    {
        private EditedDescriptorValueType _modifiedValues;
        private int _entryId;
        private string _from;
        private string _to;
        private string _example;
        private Priority _priority;
        private EditedItemState _state;

        private EditedEntryDescriptor()
        {
            //
        }

        public static EditedEntryDescriptor Create(Entry entry, Priority priority)
        {
            EditedEntryDescriptor result = new EditedEntryDescriptor();
            result._entryId = entry.Id;
            result._from = entry.From;
            result._to = entry.To;
            result._example = entry.Example;
            result._priority = priority;
            result._state = EditedItemState.Normal;

            return result;
        }

        public static EditedEntryDescriptor CreateNew()
        {
            EditedEntryDescriptor result = new EditedEntryDescriptor();
            result._state = EditedItemState.New;
            result._priority = Data.Priority.Normal;

            return result;
        }

        public static EditedEntryDescriptor CreateRemoved(int entryId)
        {
            EditedEntryDescriptor result = new EditedEntryDescriptor();
            result.State = EditedItemState.Deleted;
            result._entryId = entryId;

            return result;
        }

        public bool IsModified()
        {
            return _modifiedValues != 0;
        }

        public bool IsModified(EditedDescriptorValueType editedDescriptorValueType)
        {
            return (_modifiedValues & editedDescriptorValueType) == editedDescriptorValueType;
        }

        public string From
        {
            get
            {
                return _from;
            }
            set
            {
                if (String.IsNullOrEmpty(_from) && String.IsNullOrEmpty(value))
                {
                    return;
                }

                if (_from != value)
                {
                    _from = value;
                    _modifiedValues |= EditedDescriptorValueType.From;
                }
            }
        }

        public string To
        {
            get
            {
                return _to;
            }
            set
            {
                if (String.IsNullOrEmpty(_to) && String.IsNullOrEmpty(value))
                {
                    return;
                }

                if (_to != value)
                {
                    _to = value;
                    _modifiedValues |= EditedDescriptorValueType.To;
                }
            }
        }

        public string Example
        {
            get
            {
                return _example;
            }
            set
            {
                if (String.IsNullOrEmpty(_example) && String.IsNullOrEmpty(value))
                {
                    return;
                }

                if (_example != value)
                {
                    _example = value;
                    _modifiedValues |= EditedDescriptorValueType.Example;
                }
            }
        }

        public Priority Priority
        {
            get
            {
                return _priority;
            }
            set
            {
                if (value != _priority)
                {
                    _priority = value;
                    _modifiedValues |= EditedDescriptorValueType.Priority;
                }
            }
        }

        internal EditedItemState State
        {
            get { return _state; }
            set { _state = value; }
        }

        public int Id
        {
            get
            {
                if (_state == EditedItemState.New)
                {
                    throw new InvalidOperationException();
                }

                return _entryId;
            }
        }
    }
}
